//
//  DISAorderDetailViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/14.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "DISAorderDetailViewController.h"

@interface DISAorderDetailViewController ()
@property (weak, nonatomic) IBOutlet UILabel *timeLB;
@property (weak, nonatomic) IBOutlet UILabel *namelb;
@property (weak, nonatomic) IBOutlet UILabel *phoneLB;
@property (weak, nonatomic) IBOutlet UILabel *addressLB;
@property (weak, nonatomic) IBOutlet UILabel *titlLB;
@property (weak, nonatomic) IBOutlet UILabel *numberLB;
@property (weak, nonatomic) IBOutlet UIImageView *myimage;
@property (weak, nonatomic) IBOutlet UIButton *quxiaobut;
@property(nonatomic,assign)NSInteger dayind;

@end

@implementation DISAorderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    self.title = @"订货单详情";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    
    
    [self request];
    // Do any additional setup after loading the view from its nib.
}

- (IBAction)quxioaaction:(UIButton *)sender {
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"id"] = NOTNIL(_orderid);
    [NetWorkTool POST:canclePlaceOrder param:param success:^(id dic) {
        ShowToastWithText(@"取消成功");
        [self request];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}
-(void)request{
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"id"] = NOTNIL(_orderid);
    [NetWorkTool POST:agentOrderDetailr param:param success:^(id dic) {
        KMyLog(@"sdfdsfasf%@",dic);
        NSDictionary *mydic = [dic objectForKey:@"data"];
        self.timeLB.text =[NSString stringWithFormat:@"%@",[mydic objectForKey:@"create_time"]];
        self.namelb.text =[NSString stringWithFormat:@"%@",[mydic objectForKey:@"name"]];
        self.phoneLB.text =[NSString stringWithFormat:@"%@",[mydic objectForKey:@"phone"]];
        self.titlLB.text =[NSString stringWithFormat:@"%@",[mydic objectForKey:@"parts_details"]];
        self.numberLB.text =[NSString stringWithFormat:@"%@个",[mydic objectForKey:@"parts_count"]];
        self.addressLB.text =[NSString stringWithFormat:@"%@",[mydic objectForKey:@"address"]];

        self.dayind =[[mydic objectForKey:@"parts_count"] integerValue];
        
        
        NSString *timestr  = [self dateConversionTimeStamp:[self nsstringConversionNSDate:[mydic objectForKey:@"create_time"]]];
        self.quxiaobut.hidden = YES;

        if ([self timeAgoWithDatedfdsfsf:timestr]) {
            self.quxiaobut.hidden = NO;
            if ([[mydic objectForKey:@"state"] integerValue]  == 1) {
                self.quxiaobut.hidden = YES;
            }
        }
        if ([[mydic objectForKey:@"image_url"] hasPrefix:@"http"]) {
            [self.myimage sd_setImageWithURL:[NSURL URLWithString:[mydic objectForKey:@"image_url"]] placeholderImage:defaultImg];
        }else{
            sdimg(self.myimage, [mydic objectForKey:@"image_url"]);
            
        }
        
        
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
    
    
    
}

-(NSDate *)nsstringConversionNSDate:(NSString *)dateStr
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *datestr = [dateFormatter dateFromString:dateStr];
    return datestr;
}
-(NSString *)dateConversionTimeStamp:(NSDate *)date
{
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]*1000];
    return timeSp;
}

- (BOOL)timeAgoWithDatedfdsfsf:(NSString*)date
{
    
    date=[NSString stringWithFormat:@"%@",date];
    
    NSString *datrStr = [NSString stringWithFormat:@"%@",date];
    NSString*time =[datrStr substringToIndex:datrStr.length-3];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyy-MM-dd HH:mm:ss"];
    NSDate*confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[time intValue]];
    
    double deltaSeconds = fabs([confromTimesp timeIntervalSinceDate:[NSDate date]]);
    double deltaMinutes = deltaSeconds / 60.0f;
    
    if (deltaMinutes < (24 * 60 * self.dayind)){
        
        //        int day = (int)floor(deltaMinutes/(60 * 24));
        return YES;
        
    }else{
        return NO;
        
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
